package com.caocaog.market.service.impl;

import com.caocaog.market.service.*;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 商品缓存清除
 *
 * @author chechen
 * @date 2022-04-23
 */
@Slf4j
@Service
@Transactional
public class GoodsCleanCacheServiceImpl implements GoodsCleanCacheService {

    private RedisUtil redisUtil;
    private GoodsBaseService goodsBaseService;
    private GoodsAttrService goodsAttrService;
    private GoodsSpecService goodsSpecService;
    private GoodsCategoriesService goodsCategoriesService;

    /**
     * 根据店铺id 删除 GOODS_LIST_BY_CATEGORIES_ID 缓存
     *
     * @param shopId 店铺id
     */
    @Override
    public void cleanCacheGoodsInfoByShopId(String shopId) {
        // 根据店铺id获取状态为显示的商品分类 再删除 GOODS_LIST_BY_CATEGORIES_ID 缓存
        this.redisUtil.del(this.goodsCategoriesService.getShowGoodsCategoriesByShopId(shopId).stream().map(
                goodsCategories -> CacheConstant.GOODS_LIST_BY_CATEGORIES_ID + CommonConstant.DOUBLE_COLON + goodsCategories.getId()
        ).toArray(String[]::new));
    }

    /**
     * 根据商品分类id 删除 GOODS_LIST_BY_CATEGORIES_ID 缓存
     *
     * @param categoriesId 商品分类id
     */
    @Override
    public void cleanCacheGoodsInfoByCategoriesId(String categoriesId) {
        // 删除 GOODS_LIST_BY_CATEGORIES_ID 缓存
        this.redisUtil.del(CacheConstant.GOODS_LIST_BY_CATEGORIES_ID + CommonConstant.DOUBLE_COLON + categoriesId);
    }

    /**
     * 根据店铺id 删除 GOODS_CATEGORIES_LIST_BY_SHOP_ID GOODS_CATEGORIES_LIST_BY_SHOP_ID 缓存
     *
     * @param shopId 店铺id
     */
    @Override
    public void cleanCacheGoodsCategories(String shopId) {
        this.cleanCacheGoodsCategories(shopId, null);
    }

    public void cleanCacheGoodsCategories(String shopId, String[] categoriesIds) {
        if (categoriesIds == null)
            // 根据店铺ids获取商品分类ids
            categoriesIds = this.goodsCategoriesService.getGoodsCategoriesIdsByShopIds(shopId);

        // 删除 GOODS_CATEGORIES_LIST_BY_SHOP_ID 缓存
        this.redisUtil.del(CacheConstant.GOODS_CATEGORIES_LIST_BY_SHOP_ID + CommonConstant.DOUBLE_COLON + shopId);
        // 删除 GOODS_CATEGORIES_INFO 缓存
        this.redisUtil.del(Arrays.stream(categoriesIds).map(categoriesId ->
                CacheConstant.GOODS_CATEGORIES_INFO + CommonConstant.DOUBLE_COLON + categoriesId
        ).toArray(String[]::new));
    }

    /**
     * 根据店铺id 删除 GOODS_BASE_LIST_BY_CATEGORIES_ID GOODS_BASE_INFO 缓存
     *
     * @param shopId 店铺id
     */
    @Override
    public void cleanCacheGoodsBase(String shopId) {
        this.cleanCacheGoodsBase(shopId, null, null);
    }

    private void cleanCacheGoodsBase(String shopId, String[] categoriesIds, String[] goodsIds) {
        if (categoriesIds == null)
            // 根据店铺ids获取商品分类ids
            categoriesIds = this.goodsCategoriesService.getGoodsCategoriesIdsByShopIds(shopId);
        if (goodsIds == null)
            // 根据商品分类ids获取商品ids
            goodsIds = this.goodsBaseService.getGoodsIdsByCategoriesIds(categoriesIds);

        // 删除 GOODS_BASE_LIST_BY_CATEGORIES_ID 缓存
        this.redisUtil.del(Arrays.stream(categoriesIds).map(categoriesId ->
                CacheConstant.GOODS_BASE_LIST_BY_CATEGORIES_ID + CommonConstant.DOUBLE_COLON + categoriesId
        ).toArray(String[]::new));
        // 删除 GOODS_BASE_INFO 缓存
        this.redisUtil.del(Arrays.stream(goodsIds).map(goodsId ->
                CacheConstant.GOODS_BASE_INFO + CommonConstant.DOUBLE_COLON + goodsId
        ).toArray(String[]::new));
    }

    /**
     * 根据店铺id 删除 GOODS_SPEC_LIST_BY_GOODS_ID GOODS_SPEC_INFO 缓存
     *
     * @param shopId 商品id
     */
    @Override
    public void cleanCacheGoodsSpec(String shopId) {
        this.cleanCacheGoodsSpec(shopId, null, null);
    }

    private void cleanCacheGoodsSpec(String shopId, String[] goodsIds, String[] specIds) {
        if (goodsIds == null) {
            // 根据店铺ids获取商品分类ids
            String[] categoriesIds = this.goodsCategoriesService.getGoodsCategoriesIdsByShopIds(shopId);
            // 根据商品分类ids获取商品ids
            goodsIds = this.goodsBaseService.getGoodsIdsByCategoriesIds(categoriesIds);
        }
        if (specIds == null) {
            // 根据商品ids获取商品规格ids
            specIds = this.goodsSpecService.getGoodsSpecIdsByGoodsIds(goodsIds);
        }

        // 删除 GOODS_SPEC_LIST_BY_GOODS_ID 缓存
        this.redisUtil.del(Arrays.stream(goodsIds).map(goodsId ->
                CacheConstant.GOODS_SPEC_LIST_BY_GOODS_ID + CommonConstant.DOUBLE_COLON + goodsId
        ).toArray(String[]::new));
        // 删除 GOODS_SPEC_INFO 缓存
        this.redisUtil.del(Arrays.stream(specIds).map(specId ->
                CacheConstant.GOODS_SPEC_INFO + CommonConstant.DOUBLE_COLON + specId
        ).toArray(String[]::new));
    }

    /**
     * 根据店铺id 删除 GOODS_ATTR_LIST_BY_GOODS_ID GOODS_ATTR_INFO 缓存
     *
     * @param shopId 店铺id
     */
    @Override
    public void cleanCacheGoodsAttr(String shopId) {
        this.cleanCacheGoodsAttr(shopId, null, null);
    }

    private void cleanCacheGoodsAttr(String shopId, String[] goodsIds, String[] attrIds) {
        if (goodsIds == null) {
            // 根据店铺ids获取商品分类ids
            String[] categoriesIds = this.goodsCategoriesService.getGoodsCategoriesIdsByShopIds(shopId);
            // 根据商品分类ids获取商品ids
            goodsIds = this.goodsBaseService.getGoodsIdsByCategoriesIds(categoriesIds);
        }
        if (attrIds == null) {
            // 根据商品ids获取商品属性ids
            attrIds = this.goodsAttrService.getGoodsAttrIdsByGoodsIds(goodsIds);
        }

        // 删除 GOODS_ATTR_LIST_BY_GOODS_ID 缓存
        this.redisUtil.del(Arrays.stream(goodsIds).map(goodsId ->
                CacheConstant.GOODS_ATTR_LIST_BY_GOODS_ID + CommonConstant.DOUBLE_COLON + goodsId
        ).toArray(String[]::new));
        // 删除 GOODS_ATTR_INFO 缓存
        this.redisUtil.del(Arrays.stream(attrIds).map(attrId ->
                CacheConstant.GOODS_ATTR_INFO + CommonConstant.DOUBLE_COLON + attrId
        ).toArray(String[]::new));
    }

    /**
     * 根据店铺id 删除关于店铺内所有商品的缓存
     *
     * @param shopId 店铺id
     */
    @Override
    public void cleanCacheGoodsAll(String shopId) {
        // 根据店铺ids获取商品分类ids
        String[] categoriesIds = this.goodsCategoriesService.getGoodsCategoriesIdsByShopIds(shopId);
        if (categoriesIds.length == 0) return;
        // 根据商品分类ids获取商品ids
        String[] goodsIds = this.goodsBaseService.getGoodsIdsByCategoriesIds(categoriesIds);
        if (goodsIds.length == 0) return;
        // 根据商品ids获取商品规格ids
        String[] specIds = this.goodsSpecService.getGoodsSpecIdsByGoodsIds(goodsIds);
        // 根据商品ids获取商品属性ids
        String[] attrIds = this.goodsAttrService.getGoodsAttrIdsByGoodsIds(goodsIds);

        this.cleanCacheGoodsInfoByShopId(shopId);
        this.cleanCacheGoodsCategories(shopId, categoriesIds);
        this.cleanCacheGoodsBase(shopId, categoriesIds, goodsIds);
        this.cleanCacheGoodsSpec(shopId, goodsIds, specIds);
        this.cleanCacheGoodsAttr(shopId, goodsIds, attrIds);
    }


    @Autowired
    public void setRedisUtil(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    @Autowired
    public void setGoodsBaseService(GoodsBaseService goodsBaseService) {
        this.goodsBaseService = goodsBaseService;
    }

    @Autowired
    public void setGoodsAttrService(GoodsAttrService goodsAttrService) {
        this.goodsAttrService = goodsAttrService;
    }

    @Autowired
    public void setGoodsSpecService(GoodsSpecService goodsSpecService) {
        this.goodsSpecService = goodsSpecService;
    }

    @Autowired
    public void setGoodsCategoriesService(GoodsCategoriesService goodsCategoriesService) {
        this.goodsCategoriesService = goodsCategoriesService;
    }
}
